part of _;

extension on SplashPage {
  void controller_getLine() async {
    if (YConfig.IP.isNotEmpty) return;
    if (!await yIsNetAble()) {
      yShowBuilder("网络不可用，请检查网络！！！", () => controller_getLine(), null, false, posTitle: "重试");
      return;
    }
    var errorCount = 0;
    var hasDone = false;
    for (var i = 0; i < IFinal.Configs_Local.config_lines.length; i++) {
      try {
        final dio = Dio();
        dio.options
          ..connectTimeout = Duration(milliseconds: YConfig.timeOut)
          ..receiveTimeout = Duration(milliseconds: YConfig.timeOut)
          ..sendTimeout = Duration(milliseconds: YConfig.timeOut);
        final response = await dio.get<String>("${IFinal.Configs_Local.config_lines[i]}?v=${DateTime.now().millisecondsSinceEpoch}");
        if (hasDone) return;
        hasDone = true;
        if (YConfig.IP.isNotEmpty) return;
        if (response.statusCode == HttpStatus.ok && response.data != null && response.data!.isNotEmpty) {
          yLog(response.data!);
          final data = json.decode(response.data!);
          final packageName = (await PackageInfo.fromPlatform()).packageName;
          dynamic platformData;
          switch (defaultTargetPlatform) {
            case TargetPlatform.windows:
              platformData = yIsWeb ? data["web_pc"] : data["windows"];
              break;
            case TargetPlatform.macOS:
              platformData = yIsWeb ? data["web_pc"] : data["macos"];
              break;
            case TargetPlatform.linux:
              platformData = yIsWeb ? data["web_pc"] : data["linux"];
              break;
            default:
              platformData = data["web_pc"];
              break;
          }
          yCheckUpdate(
            newVersion: int.parse(platformData["version"]),
            updateContent: "app优化～",
            apkDownloadPath: data["android"]["apk_url"],
            androidAppId: packageName,
            iOSAppId: packageName,
            onNewest: () async {
              IFinal.initConfig_Server(data["config"]);
              // 紧急公告
              if (IFinal.Configs_Server.notice.isNotEmpty) {
                yShowBuilder(IFinal.Configs_Server.notice, () => yFinishApplication(), null, false, posTitle: "退出");
                return;
              }
              // 线路
              ySave(IFinal.Saves.lines, json.encode(IFinal.Configs_Server.lines));
              final linesIndex = int.parse(await yRead(IFinal.Saves.linesIndex) ?? "0");
              IFinal.Others.BaseIP = (linesIndex + 1 > IFinal.Configs_Server.lines.length) ? IFinal.Configs_Server.lines.first : IFinal.Configs_Server.lines[linesIndex];
              yConfigIP(IFinal.Others.BaseIP + IFinal.Urls.interface);
              yConfigUpdateIP(YConfig.IP);
              // 进入主页
              yRun(() async {
                yIntent((await AppUtils.checkIsLogon(this)) ? HomePage() : LoginPage(), intentType: YIntentType.Root_Replace);
              }, delay: 500);
            },
          );
        }
      } catch (e) {
        yToast(e.toString());
      }
      errorCount++;
      if (errorCount >= IFinal.Configs_Local.config_lines.length) yRun(() => controller_getLine(), delay: 1000);
    }
  }
}
